//
//  UIView+CNExtension.m
//  CNLawyer
//
//  Created by 李孝宇 on 15/12/16.
//  Copyright © 2015年 OsGsCn. All rights reserved.
//

#import "UIView+CNExtension.h"
#import <objc/runtime.h>

static void *targetBlock = &targetBlock;
static void *longPressTargetBlock = &longPressTargetBlock;
@implementation UIView (CNExtension)

- (void)setTargetAction:(TargetAction)targetAction{
    objc_setAssociatedObject(self, &targetBlock, targetAction, OBJC_ASSOCIATION_COPY);
}

- (TargetAction)targetAction{
    return objc_getAssociatedObject(self, &targetBlock);
}

- (void)setLongPressTargetAction:(LongPressTargetAction)longPressTargetAction{
    objc_setAssociatedObject(self, &longPressTargetBlock, longPressTargetAction, OBJC_ASSOCIATION_COPY);
}
- (LongPressTargetAction)longPressTargetAction{
    return objc_getAssociatedObject(self, &longPressTargetBlock);
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}


- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}


- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}


- (void)setK_width:(CGFloat)k_width
{
    CGRect frame = self.frame;
    frame.size.width = k_width;
    self.frame = frame;
}

- (void)setK_height:(CGFloat)k_height
{
    CGRect frame = self.frame;
    frame.size.height = k_height;
    self.frame = frame;
}

- (void)setK_left:(CGFloat)k_left
{
    CGRect frame = self.frame;
    frame.origin.x = k_left;
    self.frame = frame;
}

- (void)setK_right:(CGFloat)k_right
{
    self.k_left = k_right - self.k_width;
}

- (void)setK_top:(CGFloat)k_top
{
    CGRect frame = self.frame;
    frame.origin.y = k_top;
    self.frame = frame;
}

- (void)setK_bottom:(CGFloat)k_bottom
{
    self.k_top = k_bottom - self.k_height;
}

- (void)setK_centerX:(CGFloat)k_centerX
{
    CGPoint center = self.center;
    center.x = k_centerX;
    self.center = center;
}

- (void)setK_centerY:(CGFloat)k_centerY
{
    CGPoint center = self.center;
    center.y = k_centerY;
    self.center = center;
}

- (CGFloat)k_width
{
    return self.frame.size.width;
}

- (CGFloat)k_height
{
    return self.frame.size.height;
}

- (CGFloat)k_left
{
    return self.frame.origin.x;
}

- (CGFloat)k_right
{
    return CGRectGetMaxX(self.frame);
}

-(CGFloat)k_top
{
    return self.frame.origin.y;
}

- (CGFloat)k_bottom
{
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)k_centerX
{
    return self.center.x;
}

- (CGFloat)k_centerY
{
    return self.center.y;
}

- (UIViewController *)viewController
{
    //获取当前对象的下一响应者
    id next = [self nextResponder];
    while (next != nil) {
        //判断next对象是否为控制器
        if ([next isKindOfClass:[UIViewController class]]) {
            return next;
        }else if ([next isKindOfClass:[UIWindow class]]){
            UIViewController *vc = [(UIWindow*)next rootViewController];
            while (vc.presentedViewController) {
                vc = vc.presentedViewController;
            }
            return vc;
        }
        
        //获取next对象的下一响应这
        next = [next nextResponder];
    }
    
    return nil;
}

/** 添加点击事件 */
- (void)addTargetAction:(TargetAction)targetAction{
    self.userInteractionEnabled = YES;
    [self setTargetAction:targetAction];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetActionRes:)];
    [self addGestureRecognizer:tap];
}
/** 添加长按事件 */
- (void)addLongPressTargetAction:(LongPressTargetAction)longPressTargetAction{
    self.userInteractionEnabled = YES;
    [self setLongPressTargetAction:longPressTargetAction];
    UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPrssTargetActionRes:)];
    [self addGestureRecognizer:tap];
}

- (void)targetActionRes:(UITapGestureRecognizer *)tap{
    if (self.targetAction){
        self.targetAction(tap);
    }
}

- (void)longPrssTargetActionRes:(UILongPressGestureRecognizer *)longPress{
    if (self.longPressTargetAction) {
        self.longPressTargetAction(longPress);
    }
}

//淡入
- (void)hl_fadeInWithTime:(NSTimeInterval)time{
    self.alpha = 0;
    [UIView animateWithDuration:time animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
//淡出
- (void)hl_fadeOutWithTime:(NSTimeInterval)time{
    self.alpha = 1;
    [UIView animateWithDuration:time animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//旋转
- (void)hl_RevolvingWithTime:(NSTimeInterval)time andDelta:(CGFloat)delta{
    [UIView animateWithDuration:time animations:^{
        self.transform = CGAffineTransformMakeRotation(delta);
    }];
}




/**
 *  设置部分圆角(绝对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    self.layer.mask = shape;
}

/**
 *  设置部分圆角(相对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要设置的圆角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    self.layer.mask = shape;
}


/**
 快速设置圆角
 */
- (void)addLayerCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}

/**
 快速设置圆角边框
 */
- (void)addBorderLayer:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius{
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = borderWidth;
    [self addLayerCornerRadius:cornerRadius];
}
// 添加多个子视图
- (void)HR_addSubViews:(UIView *)view, ... {
    if (view) {
        [self addSubview:view];
        va_list argList;  //定义一个 va_list 指针来访问参数表
        va_start(argList, view);  //初始化 va_list，让它指向第一个变参，view 这里是第一个参数
        id arg;
        while ((arg = va_arg(argList, UIView *))) //调用 va_arg 依次取出 参数，它会自带指向下一个参数
        {
            if ([arg isKindOfClass:NSClassFromString(@"UIView")]) {
                [self addSubview:arg];
            }
            else{
                break;
            }
        }
        va_end(argList); // 收尾，记得关闭关闭 va_list
        
    }
}
@end
